package team.pwy.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.pwy.consts.RbmConsts;
import team.pwy.mapper.MovieMapper;
import team.pwy.mapper.TypeMapper;
import team.pwy.pojo.dto.OrderDTO;
import team.pwy.pojo.entity.Collect;
import team.pwy.pojo.entity.Movie;
import team.pwy.pojo.entity.PwyViewingHistory;
import team.pwy.vo.TypeDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

@Service
@Transactional
public class MovieServiceImpl implements MovieService {

    @Autowired
    public MovieMapper movieMapper;

    @Autowired
    public TypeMapper typeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<Movie> findHotting(Integer status,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Movie> hotting = movieMapper.findHotting(status);
        return new PageInfo<>(hotting);
    }

    @Override
    public List<Movie> findTopFive(Integer status) {
        return movieMapper.findTopFive(status);
    }

    @Override
    public Movie findById(Integer id) {
        return movieMapper.findById(id);
    }
    @Override
    public List<Movie> findByTime(){
        return movieMapper.findByTime();
    }
    @Override
    public List<Movie> findByHot(){
        return movieMapper.findByHot();
    }
    @Override
    public List<Movie> findByType(String type){
        return movieMapper.findByType(type);
    }

    @Override
    public PageInfo<Movie> findByTypes(TypeDTO typeDTO,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String,Object> typeMap = new HashMap<>();
        typeMap.put("status",typeDTO.getStatus());
//        typeMap.put("type",typeDTO.getType());
        typeMap.put("country",typeDTO.getCountry());
        typeMap.put("firstDate",typeDTO.getFirstDate());
        typeMap.put("endDate",typeDTO.getEndDate());
        typeMap.put("order",typeDTO.getOrder());
        typeMap.put("search",typeDTO.getSearch());
//        typeMap.put("grossed",typeDTO.getGrossed());

//        List<Integer> byType = new ArrayList<>();

//            Map<String,String> typeMape = new HashMap<>();
//            typeMape.put("type", typeDTO.getType());
//            byType = typeMapper.findByType(typeMape);

//        if (byType.size() > 0){
//            List<Integer> list = new ArrayList<>();
//            list.add(1);

//            typeMap.put("type",typeDTO.getType());
//        }else {
//            byType.add(0);
//            byType.add(2);
            typeMap.put("type",typeDTO.getType());
//        }
//        typeMap.put("type",byType);
        List<Movie> movies= movieMapper.findByTypes(typeMap);
        return new PageInfo<>(movies);
    }

    @Override
    public List<Movie> findByActive() {
        return movieMapper.findByActive();
    }

    @Override
    public List<Movie> findByDesire() {
        return movieMapper.findByDesire();
    }

    @Override
    public List<Movie> findByScore(){
        return movieMapper.findByScore();
    }
    //添加收藏
//   @RabbitHandler
//   @RabbitListener(queues = RbmConsts.SEC_QUEUE_COLLECT)
//   @Async
//    public void addCollect(Map<String,Integer> map) {
//       System.out.println(map);
//       if (map.get("status") == 0) {
//           Integer userId = map.get("userId");
//           Integer id = map.get("movieId");
//           Movie movie = movieMapper.findById(id);
//           Collect collect = new Collect(id, userId, movie.getId(), movie.getTitle(), movie.getPosterImg(), movie.getPrice(), movie.getStatus(), movie.getCreateTime());
//           movieMapper.addCollect(collect);
//       } else {
//           Integer userId = (Integer) map.get("userId");
//           Integer id = (Integer) map.get("movieId");
//           movieMapper.delCollect(id, userId);
//       }
//   }
    @Override
    public Integer delCollect(Integer id,Integer userId){
        movieMapper.delCollect(id,userId);
        return null;
    }
    @Override
    public List<Collect> lookCollect(Integer userId,Integer pageNum, Integer pageSize){
        PageHelper.
                startPage(pageNum,pageSize);
        return movieMapper.lookCollect(userId);
    }

    @Override
    public PageInfo<PwyViewingHistory> lookHistory(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(movieMapper.lookHistory(userId));
    }
    public List<Movie> findByStatus(Integer status) {
        return movieMapper.findByStatus(status);
    }

    @Override
    public List<Movie> findAll() {
        return movieMapper.findAll();
    }
}
